package com.training1.core.modules.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.training1.core.common.entity.RespJson;
import com.training1.core.common.entity.UserEntity;
import com.training1.core.common.global.signIn.LoginInfoUtil;
import com.training1.core.common.utils.framework.RespJsonFactory;
import com.training1.core.common.utils.security.MD5;
import com.training1.core.modules.dao.UserDao;

/**
 * 登陆类
 * @author hl
 *
 */
@Service
@Transactional
public class LoginService {

	private static final Logger log = LoggerFactory.getLogger(LoginService.class);
	
	@Autowired
	private UserDao userDao;
	
	public RespJson login(HttpServletRequest request,UserEntity entity){
		
		if(!inLoginTime()){
			return RespJsonFactory.buildFailure("login.inValidTimeError");
		}
		
		//账号密码判定
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userAccount", entity.getUserAccount());
		UserEntity user = userDao.findUser(map);
		if(user!=null){
			log.debug("账号验证成功");
			try {
				String password = MD5.getMD5(entity.getPassword());
				if(user.getPassword().equals(password)){
					log.debug("密码验证成功");
					LoginInfoUtil.setLoginInfoToSession(request, user, "");
					return RespJsonFactory.buildSuccess();
				}else {
					return RespJsonFactory.buildFailure("login.error");
				}
			} catch (Exception e) {
				log.debug("账号密码验证失败", e);
			}
		}
		return RespJsonFactory.buildFailure("login.error");
	}
	
	public RespJson passwordUpdate(HttpServletRequest request, String password, String newpassword){
		//获取session中存入的用户名
		String username = LoginInfoUtil.getLoginInfo().getLoginName();
//		UserEntity username1 = LoginInfoUtil.getLoginInfo().getLoginUser();
//		String user1 = username1.getUserAccount();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userAccount", username);
		UserEntity user = userDao.findUser(map);
		//获取原密码
		String userPW = user.getPassword();
		if(userPW.equals(MD5.getMD5(password))){
			log.debug("密码验证成功");
			//密码修改
			newpassword = MD5.getMD5(newpassword);
			map.put("newpassword", newpassword);
			userDao.updatePassword(map);
			
			return RespJsonFactory.buildSuccess();
		}else{
			//密码不正确
			log.debug("密码验证失败");
			return RespJsonFactory.buildFailure("login.passwordError");
		}
		
	}
	
	public boolean inLoginTime(){
		//登录时间判定
		/*SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		//取得系统参数(开始结束时间)
		String begin = userDao.findTimeBegin().replace(":", "");
		String end = userDao.findTimeEnd().replace(":", "");
			
		//取得服务器时间
		Date date = new Date();
		String nowDate = sdf.format(date).replace(":", "");
			
		//比较时间
		if(begin.compareTo(nowDate)>0 || end.compareTo(nowDate)<0){
			log.debug("非系统登录时间");
			return false;
		}*/
		return true;
	}
}
